﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ExportWeb.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ExportWeb type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.ContentMigration
{
    using System.Collections.Generic;

    using Engine;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Deployment;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.ContentMigration;
    using SharePoint.Wss.Webs;

    using Utils;

    [CollaborisClass(
        Title =
            "Exports a Web to a CMP.",
        ReleaseStatus =
            ReleaseStatus.Stable,
        DisableDocumentation =
            false,
        ContactAuthorEmail =
            "Support@Collaboris.co.uk",
        Summary = 
            "Exports a SharePoint web (SPWeb) to a Content Migration Package (CMP).",
        Remarks =
            "<p>The 'Do' method for this action uses the standard SharePoint content migration " +
            "API to achieve the export. " +
            "It is highly recommended that the server is running a minimum of SharePoint " +
            "Service Pack 1.</p>" +
           @"This action can easily be paired with <a href=""C:\Temp\safDoc\Collaboris.Saf.Actions.Wss.ContentMigration.ImportWeb.html"">Import Web</a>" +
            " to move content between sites and/or farms.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C635}")]
    public class ExportWeb : WSSActionBase<WebExportSettings>
    {
        #region Fields (1) 

        ContentMigrationHelper helper = new ContentMigrationHelper();

        #endregion Fields 

        #region Methods (6) 

        /// <summary>
        /// When called causes the list passed in to be exported to a 
        /// CMP Package to a specified file.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, WebExportSettings entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");

            TargetWeb.AllowUnsafeUpdates = true;

            // TODO : Sort!
            //WSSLib.ContentMigrationHelper.ExportWebListsOnly(entity.ExportSettings, currentWeb);

            // TODO : Pass these params in.
            SPIncludeDescendants descs = SPIncludeDescendants.None;
            if (entity.IgnoreSubWebs)
            {
                descs = SPIncludeDescendants.Content;
            }
            else
            {
                descs = SPIncludeDescendants.All;
            }

            helper.ExportWeb(entity.ExportSettings, TargetWeb, descs);

            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
            
        }
		
        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, WebExportSettings entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            // ValidateStringForNull(entity.ListName, "ContentTypeToListMapping.ListName", ref errors);
            
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }


        #endregion Methods 
    }
}